﻿using System.Collections.Specialized;
using DBFile.Core.Interfaces;
using DBFile.Providers.Impl;

namespace DBFile.Providers
{
    public class AzureBlobProvider : DBFileProviderBase
    {
        protected override string ProviderDescription
        {
            get { return ProviderDescriptionMessage; }
        }

        protected override void OnAfterInitialize(NameValueCollection config)
        {
            AccountName = GetConfigSettingOrThrow(config, AccountNameKey, AccountNameMissingMessage);
            AccountShareKey = GetConfigSettingOrThrow(config, AccountShareKeyKey, AccountShareKeyMissingMessage);
            BlobStorageEndpoint = GetConfigSettingOrThrow(config, BlobStorageEndpoingKey, BlobStorageEndpoingMissingMessage);
            IsPrivateContainer = GetConfigSettingAsBool(config, IsPrivateContainerKey);
        }

        protected override IDBFileProvider Provider
        {
            get
            {
                lock (padLock)
                {
                    if (_provider == null)
                    {
                        _provider = new AzureBlobStorage(AccountName, AccountShareKey, BlobStorageEndpoint, IsPrivateContainer);
                    }
                }
                return _provider;
            }
        }
        
        internal string AccountName { get; private set; }
        internal string AccountShareKey { get; private set; }
        internal string BlobStorageEndpoint { get; private set; }
        internal bool IsPrivateContainer { get; private set; }

        private const string ProviderDescriptionMessage = "Windows Azure Blob DBFile Provider";
        private const string AccountNameKey = "accountName";
        private const string AccountNameMissingMessage = "Account name is not specified";
        private const string AccountShareKeyKey = "accountShareKey";
        private const string IsPrivateContainerKey = "isPrivateContainer";
        private const string AccountShareKeyMissingMessage = "Account Share Key is not specified";
        private const string BlobStorageEndpoingKey = "blobStorageEndpoint";
        private const string BlobStorageEndpoingMissingMessage = "Blob Storage Endpoint is not specified";
    }
}
